/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

package com.bullionxpress.client.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import com.bullionxpress.client.application.ClientGlobals;
import com.bullionxpress.client.view.ModifyProfilePanel;
import com.bullionxpress.client.view.RegistrarPanel;
import com.bullionxpress.shared.model.Account;
import com.bullionxpress.shared.model.Command;
import com.bullionxpress.shared.model.User;
import com.bullionxpress.shared.service.AccountDatabase;
import com.bullionxpress.shared.service.DuplicateAccountIDException;
import com.bullionxpress.shared.service.DuplicateUserIDException;
import com.bullionxpress.shared.service.InvalidAccountIDFormatException;
import com.bullionxpress.shared.service.InvalidPasswordException;
import com.bullionxpress.shared.service.InvalidUserIDFormatException;
import com.bullionxpress.shared.service.LoginService;
import com.bullionxpress.shared.service.MissingInformationException;
import com.bullionxpress.shared.service.UserDatabase;
import com.bullionxpress.shared.service.UserNotFoundException;
import com.bullionxpress.shared.util.Log;
import com.bullionxpress.shared.util.Utility;

/**
 *
 * @author BullionXpress.com
 */
public class ModifyProfileController implements ActionListener {

    ModifyProfilePanel panel = null;
    JDialog dialog = null;

    public void setView(ModifyProfilePanel panel, JDialog dialog)
    {
        this.panel = panel;
        this.dialog = dialog;
    }
    
    public void actionPerformed(ActionEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
        if( e.getActionCommand().equals(Command.SAVE_USER.toString())){
            doSaveUser();
        }
    }

    public void doSaveUser()
    {
        try {
            if( panel.getProvince().equals("")){
                JOptionPane.showMessageDialog(null, "Invalid Province.");
                return;
            }
            if( panel.getCountry().equals("")){
                JOptionPane.showMessageDialog(null, "Invalid Country.");
                return;
            }
            if( panel.getCity().equals("")){
                JOptionPane.showMessageDialog(null, "Invalid City.");
                return;
            }
            if( panel.getPostalCode().equals("")){
                JOptionPane.showMessageDialog(null, "Invalid Postal Code.");
                return;
            }

            if( panel.getFirstName().equals("")){
                JOptionPane.showMessageDialog(null, "Invalid First Name.");
                return;
            }
            if( panel.getLastName().equals("")){
                JOptionPane.showMessageDialog(null, "Invalid Last Name.");
                return;
            }
            if( panel.getUserID().equals("")){
                JOptionPane.showMessageDialog(null, "Invalid User ID.");
                return;
            }
            if( !Utility.isValidEmail(panel.getEmail())){
                JOptionPane.showMessageDialog(null, "Invalid Email.");
                return;
            }
            if( !Utility.isValidTelephone(panel.getTelephone())){
                JOptionPane.showMessageDialog(null, "Invalid Telephone.");
                return;
            }
            if( !Utility.isValidUserID(panel.getUserID())){
                JOptionPane.showMessageDialog(null, "Invalid User ID.");
                return;
            }
            if( !Utility.isValidPassword(panel.getPassword())){
                JOptionPane.showMessageDialog(null, "Invalid Password.");
                return;
            }
            if( !panel.getPassword().equals(panel.getConfirmPassword()) ){
                JOptionPane.showMessageDialog(null, "Passwords do not match.");
                return;
            }

            UserDatabase reg = ClientGlobals.userDatabase;
            AccountDatabase adb = ClientGlobals.accountDatabase;
            User u = new User();

            Account account = new Account();
            account.setId("A" + Long.toHexString(Utility.generateId()));
            account.setName("Default");
            account.setOwner(panel.getUserID());
            adb.register(account);


            u.setFirstName(panel.getFirstName());
            u.setLastName(panel.getLastName());
            u.setPassword(panel.getPassword().toLowerCase());
            u.setAddress(panel.getAddress());
            u.setTelephone(panel.getTelephone());
            u.setUserID(panel.getUserID().toLowerCase());
            u.setCreateDate(System.currentTimeMillis());
            u.setEmail(panel.getEmail().toLowerCase());
            u.setCity(panel.getCity());
            u.setPostalCode(panel.getPostalCode());
            u.setCountry(panel.getCountry());
            u.setProvince(panel.getProvince());
            u.setAccount(account);

            reg.save(u);
            JOptionPane.showMessageDialog(null, "User Profile Saved Successfully.\n");


            dialog.dispose();
            
        } catch (UserNotFoundException ex) {
            Logger.getLogger(ModifyProfileController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MissingInformationException ex) {
            JOptionPane.showMessageDialog(null, "Missing Information");
            Logger.getLogger(ModifyProfileController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidUserIDFormatException ex) {
            JOptionPane.showMessageDialog(null, "Invalid User ID Format");
            Logger.getLogger(ModifyProfileController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DuplicateAccountIDException ex) {
            JOptionPane.showMessageDialog(null, "Duplicate Account ID");
            Logger.getLogger(ModifyProfileController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidAccountIDFormatException ex) {
            JOptionPane.showMessageDialog(null, "Invalid Account ID");
            Logger.getLogger(ModifyProfileController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog(null, "Connection Exception");
            Logger.getLogger(ModifyProfileController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
